<div class="register form">
<?php echo $this->Form->create('User', array('action' => 'register', 'id' => 'formRegister'));?>
	<fieldset>
		<legend><?php echo $register ?></legend>
	<?php
		echo $this->Form->input('username', array('id' => 'username', 'after' => '<span></span>', 'class' => 'validate[required, custom[onlyLetterNumber]]'));
		echo $this->Form->input('name', array('class' => 'validate[required]'));
		echo $this->Form->input('password', array('label' => __('Password', true), 'type' => 'password', 'id' => 'password'));
                echo $this->Form->input('cpwd', array('label' => __('Confirm Password', true), 'class' => 'validate[required,equals[password]]', 'type' => 'password', 'div' => false, 'before' => '<div class="input password required">', 'after' => '</div>'));
		echo $this->Form->input('email', array('id' => 'email', 'class' => 'validate[required,custom[email]]'));
	?>
	</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
</div>
<script type="text/javascript">
    $(document).ready(function() {
        //validate 
        $("#formRegister").validationEngine();
        
       $("#username").focusout(function() {
           var username = $('#username').val();
           if (username.length > 0) {
           $.ajax({
                url: '<?php echo $this->Html->url(array('controller' => 'users', 'action' => 'checkUsername')); ?>/' + username,
                dataType: 'json',
                success:function(data){
                       //alert(data); 
                        if(data.exist == 1){
                                $('#username').focus();
                                //$('#username').select();
                        }else{
                            $('#cusername').text('');
                        }
                }
           });
           }
           
       });

        $("#email").focusout(function() {
           $.ajax({
                url: '<?php echo $this->Html->url(array('controller' => 'users', 'action' => 'checkEmail')); ?>/' + $('#email').val(),
                dataType: 'json',
                success:function(data){
                       //alert(data);
                        if(data.exist == 1){
                           //$('#email').focus();
                        }else{
                            $('#cusername').text('');
                        }
                }
           })

       });
    });
</script>